package com.ruoyi.ysg.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.ysg.domain.*;
import com.ruoyi.ysg.service.IYsgCourseService;
import com.ruoyi.ysg.service.IYsgTeacherService;
import com.ruoyi.ysg.utils.YsgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教学工作Controller
 *
 * @author xsz
 * @date 2025-11-21
 */
@RestController
@RequestMapping("/ysg/work")
public class YsgWorkController extends BaseController {
    @Autowired
    private IYsgTeacherService ysgTeacherService;
    @Autowired
    private IYsgCourseService ysgCourseService;

    /**
     * 查询今日课程安排
     */
    @PreAuthorize("@ss.hasRole('ysg:work')")
    @PostMapping("/getTodayCourses")
    public AjaxResult getTodayCourses(@RequestBody YsgCourseSchedule vo)
    {
        List<YsgCourseSchedule> list = ysgCourseService.getTodayCourses(vo);
        return AjaxResult.success(list);
    }
    /**
     * 查询课程签到记录
     */
    @PreAuthorize("@ss.hasRole('ysg:work')")
    @GetMapping("/getSignInRecords")
    public TableDataInfo getSignInRecords(YsgSignRecord ysgSignRecord)
    {
        ysgSignRecord.setTeacherId(YsgUtils.getTeacherId());
        startPage();
        List<YsgSignRecord> list = ysgCourseService.getSignInRecords(ysgSignRecord);
        return getDataTable(list);
    }

    /**
     * 查询单个打卡记录
     */
    @PreAuthorize("@ss.hasRole('ysg:work')")
    @GetMapping("/getSignInRecord")
    public AjaxResult getSignInRecord(Long id)
    {
        YsgSignRecord record = ysgCourseService.getSignInRecordById(id);
        return AjaxResult.success(record);
    }

    /**
     * 查询课程关联学生列表
     */
    @PreAuthorize("@ss.hasRole('ysg:work')")
    @GetMapping("/associatedStudentList")
    public TableDataInfo associatedStudentList(YsgCourseStudent ysgCourseStudent)
    {
        ysgCourseStudent.setAssociated("associated");
        startPage();
        List<YsgStudent> list = ysgCourseService.selectAssociatedStudentList(ysgCourseStudent);
        return getDataTable(list);
    }

    /**
     * 签到打卡
     */
    @PreAuthorize("@ss.hasRole('ysg:work')")
    @PostMapping("/signIn")
    public AjaxResult signIn(@RequestBody YsgSignRecord ysgSignRecord)
    {
        ysgCourseService.signIn(ysgSignRecord);
        return AjaxResult.success();
    }

    /**
     * 批量签到打卡
     */
    @PreAuthorize("@ss.hasRole('ysg:work')")
    @PostMapping("/batchSignIn")
    public AjaxResult batchSignIn(@RequestBody String paramJson)
    {
        List<YsgSignRecord> signRecords = JSONArray.parseArray(paramJson, YsgSignRecord.class);
        ysgCourseService.signIn(signRecords);
        return AjaxResult.success();
    }

    /**
     * 课后评价
     */
    @PreAuthorize("@ss.hasRole('ysg:work')")
    @PostMapping("/rate")
    public AjaxResult rate(@RequestBody YsgSignRecord ysgSignRecord)
    {
        ysgCourseService.rate(ysgSignRecord);
        return AjaxResult.success();
    }
}
